package net.pinnoocle.playservice.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.pinnoocle.playcommon.utils.HelpUtils;
import net.pinnoocle.playdao.dao.OrderDao;
import net.pinnoocle.playdao.entity.Shop;
import net.pinnoocle.playdao.entity.ShopUser;
import net.pinnoocle.playdao.entity.admin.homepage.ActiveUserTopVo;
import net.pinnoocle.playdao.entity.admin.homepage.TodayDataVo;
import net.pinnoocle.playdao.entity.admin.homepage.TotalDataVo;
import net.pinnoocle.playdao.entity.shopPc.homePage.DoubleVo;
import net.pinnoocle.playdao.entity.shopPc.homePage.NumberVo;
import net.pinnoocle.playservice.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;


/**
 * @author ye
 * @Description
 * @create 2021-12-06 11:29
 */
@Service("adminAnalysisService")
public class AdminAnalysisService {

    @Autowired
    ShopServiceImpl shopService;
    @Autowired
    ShopUserServiceImpl shopUserService;
    @Autowired
    CommonService commonService;
    @Autowired
    OrderDao orderDao;

    /**
     * 获取数据总览
     * @return
     */
    public TotalDataVo getTotalData(){
        TotalDataVo totalDataVo = new TotalDataVo();
        //今天
        String today = DateUtil.today();
        today = today + " 23:59:59";
        //昨天
        DateTime yesterdayDate = DateUtil.offsetDay(DateUtil.parse(today), -1);
        String yesterdayStr = DateUtil.format(yesterdayDate, "yyyy-MM-dd");
        yesterdayStr = yesterdayStr +  "23:59:59";
        //总店铺数
        int totalToday = shopService.count(new QueryWrapper<Shop>().lambda()
                .eq(Shop::getIsDisable, 0)
                .le(Shop::getCreatedTime, today));
        int totalYesterday = shopService.count(new QueryWrapper<Shop>().lambda()
                .eq(Shop::getIsDisable, 0)
                .le(Shop::getCreatedTime, yesterdayStr));
        NumberVo shopNumberVo = commonService.getNumberVo(totalToday, totalYesterday);
        totalDataVo.setTotalShop(shopNumberVo);

        //总用户数
        int totalUserToday = shopUserService.count(new QueryWrapper<ShopUser>().lambda()
                .le(ShopUser::getCreatedTime, today));
        int totalUserYesterday = shopUserService.count(new QueryWrapper<ShopUser>().lambda()
                .le(ShopUser::getCreatedTime, yesterdayStr));
        NumberVo userNumberVo = commonService.getNumberVo(totalUserToday, totalUserYesterday);
        totalDataVo.setTotalUser(userNumberVo);

        //累计营业额
        BigDecimal totalPriceToday = orderDao.shopOrderStatisticsByHour(null, null, today, 1);
        BigDecimal totalPriceYesterday = orderDao.shopOrderStatisticsByHour(null, null, yesterdayStr, 1);
        DoubleVo orderVo = commonService.getDoubleVo(totalPriceToday.doubleValue(), totalPriceYesterday.doubleValue());
        totalDataVo.setTotalOrder(orderVo);

        //累计订单数
        int allOrderNumToday = orderDao.getAllOrderNum(null,today);
        int allOrderNumYesterday = orderDao.getAllOrderNum(null,yesterdayStr);
        NumberVo orderNumberVo = commonService.getNumberVo(allOrderNumToday, allOrderNumYesterday);
        totalDataVo.setTotalOrderNum(orderNumberVo);
        return totalDataVo;
    }

    /**
     * 实时概况
     * @return
     */
    public TodayDataVo getTodayData(){
        TodayDataVo todayDataVo = new TodayDataVo();
        String today = DateUtil.today();
        String startToday = today + " 00:00:00";
        String endToday = today + " 23:59:59";
        //今日新增店铺数
        int shopNum = shopService.count(new QueryWrapper<Shop>().lambda()
                .eq(Shop::getIsDisable, 0)
                .ge(Shop::getCreatedTime,startToday)
                .le(Shop::getCreatedTime, endToday));
        todayDataVo.setShopNum(shopNum);

        //今日新增用户数
        int userNum = shopUserService.count(new QueryWrapper<ShopUser>().lambda()
                .ge(ShopUser::getCreatedTime, startToday)
                .le(ShopUser::getCreatedTime, endToday));
        todayDataVo.setUserNum(userNum);
        //今日收入
        BigDecimal totalAmount = orderDao.shopOrderStatisticsByHour(null, endToday, today, 1);
        todayDataVo.setTotalAmount(totalAmount.doubleValue());
        //今日店铺总订单数
        int allOrderNumToday = orderDao.getAllOrderNum(startToday,endToday);
        todayDataVo.setOrderNum(allOrderNumToday);

        //即将到期店铺
        DateTime afterWeekDate = DateUtil.offsetDay(DateUtil.parse(today), +7);
        String expireDate = DateUtil.format(afterWeekDate, "yyyy-MM-dd HH:mm:ss");
        int expireShopNum = orderDao.afterWeekDateExpireShop(expireDate);
        todayDataVo.setExpireShopNum(expireShopNum);

        //本月已完成营业额
        HashMap<String, String> monthStartAndEndTime = HelpUtils.getMonthStartAndEndTime(null);
        String startDate = monthStartAndEndTime.getOrDefault("startDate","");
        String endDate = monthStartAndEndTime.getOrDefault("endDate","");
        BigDecimal orderAmountMonth = orderDao.shopOrderStatisticsByHour(null, startDate, endDate, 1);
        todayDataVo.setTotalAmountMonth(orderAmountMonth.doubleValue());
        return todayDataVo;
    }

    /**
     * 活跃用户排行
     * @param offset
     * @return
     */
    public ActiveUserTopVo activeUserTop(Integer offset){
        return orderDao.countActiveUserTop(offset);
    }
}
